package com.itheima.controller;

import cn.hutool.core.lang.UUID;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.pojo.Member;
import com.itheima.pojo.Result;
import com.itheima.service.MemberService;
import com.itheima.utils.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import redis.clients.jedis.JedisPool;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    JedisUtil jedisUtil;
    @Autowired
    JedisPool jedisPool;
    @Reference
    MemberService memberService;

    @RequestMapping("/check")
    public Result check(@RequestBody Map map){
        try {
            String telephone = (String) map.get("telephone");
            String validateCode = (String) map.get("validateCode");
            String code = jedisUtil.get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
            if (validateCode == null || !validateCode.equals(code)) {
                return Result.error("验证码错误，请重新输入");
            }
            //根据手机号码查询是否是我们的会员
            Member member = memberService.findByTelephone(telephone);
            // 如果不是就自动注册
            if (member == null) {
                member = new Member();
                member.setPhoneNumber(telephone);
                member.setRegTime(new Date());
                member = memberService.add(member);
            }
            String token = UUID.randomUUID().toString();
            jedisUtil.addTimeLimited(token,60*60*24*7, JSON.toJSONString(member));
            if (member.getName() == null || member.getEmail() == null || member.getSex() == null || member.getIdCard() == null || member.getBirthday() == null) {
                return Result.success("fix", token);
            } else {
                return Result.success("登录成功",token);
            }
        } catch (Exception e) {
            return Result.error("登录失败");
        }
    }

    @RequestMapping("/getMember")
    public Result getMember(){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String token = request.getHeader("token");
            String redisToken = jedisPool.getResource().get(token);
            Member member = JSON.parseObject(redisToken, Member.class);
            return Result.success("回显成功", member);
        } catch (Exception e) {
            return Result.error("回显失败");
        }
    }

    @RequestMapping("/saveMember")
    public Result saveMember(@RequestBody Member member) {
        try {
            memberService.saveMember(member);
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String token = request.getHeader("token");
            jedisUtil.addTimeLimited(token,60*60*24*7, JSON.toJSONString(member));
            return Result.success("保存成功");
        } catch (Exception e) {
            return Result.error("保存失败");
        }
    }

}
